﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql;
using MySql.Data;
using MySql.Data.MySqlClient;
using MySql.Data.Entity;
using ASV.Classes;
using System.Data;
using System.Web.Security;
using System.IO;

namespace ASV.Benutzer
{
    public partial class BenutzerProfil : System.Web.UI.Page
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            Connection mycon = new Connection();
            string error = Request.QueryString["error"];
            string selectedID = HttpContext.Current.Session["kundennummer"].ToString();
            object[] values = null;
            DataTable datatable;
            int numrows;
            int numcells;

            datatable = mycon.select("SELECT kundennummer, vorname,nachname, email FROM benutzer WHERE kundennummer='"+selectedID+"'");
            values = datatable.Rows[0].ItemArray;
            
            if (error == "1")
            {
                confPass.Visible = true;
            }else
            {
                confPass.Visible = false;
            }
            
            NameID.Text = values[1].ToString();
            nachname.Text = values[2].ToString();
            eamilID.Text = values[3].ToString();

            datatable = mycon.select("SELECT p.ID,p.bildpfad,lz.lizenzID,p.Name,pt.name,p.preis,vv.versionsnummer,lz.lizenznummer,b.bestelldatum FROM `bestellung` as b JOIN lizenzverwaltung as lz on lz.lizenzID = b.lizenzID JOIN versionsverwaltung as vv on vv.vVersionsID = lz.versionsID JOIN produkte as p on p.ID = vv.ProduktID JOIN produkttyp as pt on p.prodkttype = pt.ID WHERE kundennummer_btl ='" + selectedID + "' ORDER BY `prodkttype` ASC");

            if (datatable.Rows.Count != 0)
            {
                for (int x = 0; x < datatable.Rows.Count; x++)
                {
                    values = datatable.Rows[x].ItemArray;

                    numrows = datatable.Rows.Count;
                    numcells = datatable.Columns.Count;
                    TableRow row = new TableRow();
                    TableCell cellButton = new TableCell();

                    Image img = new Image();
                    img.ImageUrl = "/../" + values[1].ToString();
                    img.Width = 100;
                    img.Height = 100;

                    cellButton.Controls.Add(img);
                    row.Cells.Add(cellButton);

                    for (int i = 3; i < numcells; i++)
                    {
                        TableCell cell = new TableCell();
                        cell.Controls.Add(new LiteralControl("" + values[i]));
                        row.Cells.Add(cell);
                    }

                    cellButton = new TableCell();
                    Button buy = new Button();
                    buy.Text = "Download";
                    buy.CssClass = "btn btn-primary";
                    buy.ID = "btnbuy_" + values[2].ToString();
                    buy.Click += new System.EventHandler(sendeAenderung);

                    cellButton.Controls.Add(buy);
                    row.Cells.Add(cellButton);

                    ProdukteUser.Rows.Add(row);
                }
            }
        }
        
        protected void sendeAenderung(object sender, EventArgs e)
        {
            Connection mycon = new Connection();
            Authentication auth = new Authentication();
            string selectedID = HttpContext.Current.Session["kundennummer"].ToString();
            DataTable datatable;
            Button clickedButton = (Button)sender;
            
            string strFileName;
            string path;
            byte[] bts;
            MemoryStream ms;

            if (clickedButton.ID.IndexOf("_") != -1)
            {
                //Download der gekauften Datei
                datatable = mycon.select("SELECT vv.dateipfad FROM `bestellung` as b JOIN lizenzverwaltung as lz on lz.lizenzID = b.lizenzID JOIN versionsverwaltung as vv on vv.vVersionsID = lz.versionsID JOIN produkte as p on p.ID = vv.ProduktID JOIN produkttyp as pt on p.prodkttype = pt.ID WHERE kundennummer_btl ='" + selectedID + "' AND lz.lizenzID = '" + clickedButton.ID.Split('_')[1] + "'");
                strFileName = (datatable.Rows[0].ItemArray[0].ToString().Split('/'))[1].ToString();// lnkDownload.Text;

                path = Server.MapPath("~/Software//" + strFileName);

                if (File.Exists(path))
                {
                    bts = System.IO.File.ReadAllBytes(path);
                    ms = new MemoryStream(bts);
                    Response.Clear();
                    Response.AddHeader("Content-Disposition", "attachment;filename=\"" + strFileName + "\"");
                    Response.TransmitFile(path);
                    Response.End();
                } 
            }
            else if (!passwort.Text.Equals("") && confPasswort.Text.Equals(""))
            {
                Response.Redirect("~/Benutzer/BenutzerProfil.aspx?error=1");
            }
            else if (passwort.Text.Equals(confPasswort.Text) && !passwort.Text.Equals(""))
            {
                confPass.Visible = false;
                mycon.insertUpdate("UPDATE benutzer SET vorname = '" + NameID.Text + "', nachname = '" + nachname.Text + "', email = '" + eamilID.Text + "', passwort = '" + confPasswort.Text + "' WHERE kundennummer='" + selectedID + "'");
                auth.logout();
                Response.Redirect("~/Start.aspx");
            }
            else
            {
                confPass.Visible = false;
                mycon.insertUpdate("UPDATE benutzer SET vorname = '" + NameID.Text + "', nachname = '" + nachname.Text + "', email = '" + eamilID.Text + "' WHERE kundennummer='" + selectedID + "'");
                HttpContext.Current.Session["vorname"] = NameID.Text;
                HttpContext.Current.Session["nachname"] = nachname.Text;
                HttpContext.Current.Session["email"] = eamilID.Text;
                Response.Redirect("~/Start.aspx");
            }
        }
    }
}